<template>
  <div>
    <!-- 筛选条件 -->
    <el-form
      ref="srarchForm"
      :inline="true"
      :model="searchMap"
      class="demo-form-inline"
      style="margin-top:20px;"
    >
      <!-- 重置要看el-form-item组件元素的prop属性上是否指定了字段名，重置才能生效 -->
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="供应商名称"></el-input>
      </el-form-item>
      <!-- 通过控制isDialog的值来判断是显示还是隐藏 -->
      <el-form-item prop="linkman" v-if="!isDialog">
        <el-input v-model="searchMap.linkman" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item prop="mobile" v-if="!isDialog">
        <el-input v-model="searchMap.mobile" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" round @click="fetchData" icon="el-icon-search" size="small">查询</el-button>
        <el-button type="primary" round @click="handleAdd" icon="el-icon-edit" size="small" v-if="!isDialog">增加</el-button>
        <el-button type="info" round plain @click="resetForm('srarchForm')" size="small" v-if="!isDialog">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格数据展示 -->
    <el-table :data="supplierList" height="420" style="width: 100%">
      <el-table-column prop="index" label="序号" type="index" width="60"></el-table-column>
      <el-table-column prop="name" label="供应商名称"></el-table-column>
      <el-table-column prop="linkman" label="联系人" width="90"></el-table-column>
      <el-table-column v-if="!isDialog" prop="mobile" label="联系电话"></el-table-column>
      <el-table-column v-if="!isDialog" prop="remark" label="备注"></el-table-column>
      <el-table-column v-if="!isDialog" label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :layout="!isDialog?'total, sizes, prev, pager, next, jumper':' prev, pager, next'"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      :total="total"
    ></el-pagination>
    <!-- 弹出新增窗口 
        title 窗口的标题
        :visible.sync 当它true的时候，窗口会被弹出
    -->
    <el-dialog title="供应商编辑" :visible.sync="dialogFormVisible" width="500px">
      <el-form
        :rules="rules"
        ref="pojoForm"
        label-width="100px"
        label-position="right"
        style="width: 400px;"
        :model="pojo"
      >
        <el-form-item label="供应商名称" prop="name">
          <el-input v-model="pojo.name"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="linkman">
          <el-input v-model="pojo.linkman"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="pojo.mobile"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="pojo.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <!-- <el-button type="primary" @click="addData('pojoForm')">确 定</el-button> -->
        <!-- 通过判断有没有id值来确定点击的是新增窗口还是编辑窗口的确定按钮，来触发不同的方法 -->
        <el-button
          type="primary"
          @click="pojo.id === null?addData('pojoForm'):updateData('pojoForm')"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { supplierRules } from "@/utils/constData";
import SupplierApi from "@/api/supplier.js";
export default {
  //接受父组件传递过来的数据，通过isDialog来判断将该组件作为页面还是弹出框进行展示，
  // 如果为true，则作为弹出框
  props: {
    isDialog: Boolean
  },
  data() {
    return {
      supplierList: [],
      currentPage: 1,
      pageSize: 30,
      total: 0,
      searchMap: {
        name: "",
        linkman: "",
        mobile: ""
      }, //条件查询绑定值-------条件值(必须得指定，否则重置会失效)
      dialogFormVisible: false, //弹框默认关闭
      pojo: {
        name: "",
        linkman: "",
        mobile: "",
        remark: ""
      }, //新增的表单元素----------------
      rules: supplierRules //规则
    };
  },
  created() {
    this.fetchData(this.currentPage, this.pageSize, this.searchMap);
  },
  methods: {
    fetchData(page, size, searchMap) {
      //   SupplierApi.getsupplierList().then(response => {
      //     const List = response.data;
      //     this.supplierList = List;
      //   });
      SupplierApi.getsupplierListPage(page, size, searchMap).then(response => {
        const List = response.data;
        this.supplierList = List.rows;
        this.total = List.total;
      });
    },
    //编辑按钮
    handleEdit(index, row) {
      this.dialogFormVisible = true;
      SupplierApi.getSupplierById(row.id).then(response => {
        const flag = response.flag;
        if (flag) {
          this.pojo = response.data;
        }
      });
    },
    //删除按钮
    handleDelete(index, row) {
      SupplierApi.deleteSupplier(row.id).then(response => {
        const deleteFlag = response.flag;
        this.$message({
          showClose: true,
          message: deleteFlag ? "删除成功" : "删除失败",
          type: deleteFlag ? "success" : "error"
        });
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.fetchData(this.currentPage, this.pageSize, this.searchMap);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData(this.currentPage, this.pageSize, this.searchMap);
    },
    //增加按钮事件
    handleAdd() {
      this.dialogFormVisible = true;
      this.$refs["pojoForm"].resetFields(); //重置将输入框清空
    },
    //重置按钮事件
    resetForm(formName) {
      //重置要看el-form-item组件元素的prop属性上是否指定了字段名，重置才能生效
      this.$refs[formName].resetFields();
    },
    //弹框确事件
    addData(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          SupplierApi.addSupplier(this.pojo).then(response => {
            const addData = response.flag;
            this.$message({
              showClose: true,
              message: addData ? "添加成功" : "添加失败",
              type: addData ? "success" : "error"
            });
            this.dialogFormVisible = false;
            this.fetchData(this.currentPage, this.pageSize, this.searchMap);
          });
        } else {
          return false;
        }
      });
    },
    //根据id更新该供应商数据
    updateData(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          SupplierApi.updateSupplier(this.pojo).then(response => {
            const updateData = response.flag;
            this.$message({
              showClose: true,
              message: updateData ? "修改成功" : "修改失败",
              type: updateData ? "success" : "error"
            });
            this.dialogFormVisible = false;
            this.fetchData(this.currentPage, this.pageSize, this.searchMap);
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>